package com.lingnan.service.impl;

import com.lingnan.service.UserStatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserStatisticsServiceImpl implements UserStatisticsService {

    // 添加微服务调用组件
    private final RestTemplate restTemplate;

    // 配置微服务端点（实际值应放在配置文件中）
    private static final String USER_SERVICE_URL = "http://user-service/api/stats";

    @Override
    public long getJobSeekerCount() {
        // 调用微服务端点
        String url = USER_SERVICE_URL + "jobSeekers/total";
        return restTemplate.getForObject(url, Long.class);
    }

    @Override
    public long getCompanyUserCount() {
        String url = USER_SERVICE_URL + "companies/total";
        return restTemplate.getForObject(url, Long.class);
    }

    @Override
    public long getHrUserCount() {
        String url = USER_SERVICE_URL + "hrs/total";
        return restTemplate.getForObject(url, Long.class);
    }

    @Override
    public Map<String, Long> getUserGrowth(int days) {
        String url = USER_SERVICE_URL + "growth?days={days}";
        return restTemplate.getForObject(url, Map.class, days);
    }

    @Override
    public long getJobSeekerCountByMonth(int month) {
        String url = USER_SERVICE_URL + "jobSeekers/monthly?month={month}";
        return restTemplate.getForObject(url, Long.class, month);
    }

    @Override
    public long getCompanyUserCountByMonth(int month) {
        String url = USER_SERVICE_URL + "companies/monthly?month={month}";
        return restTemplate.getForObject(url, Long.class, month);
    }

    @Override
    public long getHrUserCountByMonth(int month) {
        String url = USER_SERVICE_URL + "hrs/monthly?month={month}";
        return restTemplate.getForObject(url, Long.class, month);
    }

    @Override
    public long countJobSeekersByDateRange(LocalDate start, LocalDate end) {
        String url = USER_SERVICE_URL + "jobSeekers/range?start={start}&end={end}";
        return restTemplate.getForObject(url, Long.class, start, end);
    }

    @Override
    public long countCompaniesByDateRange(LocalDate start, LocalDate end) {
        String url = USER_SERVICE_URL + "companies/range?start={start}&end={end}";
        return restTemplate.getForObject(url, Long.class, start, end);
    }

    @Override
    public long countHrsByDateRange(LocalDate start, LocalDate end) {
        String url = USER_SERVICE_URL + "hrs/range?start={start}&end={end}";
        return restTemplate.getForObject(url, Long.class, start, end);
    }

    // 新增：计算增长率百分比
    @Override
    public Map<String, String> getUserGrowthPercentage(int days) {
        Map<String, Long> growth = getUserGrowth(days);
        Map<String, Long> currentCounts = new HashMap<>();

        currentCounts.put("jobSeekers", getJobSeekerCount());
        currentCounts.put("companies", getCompanyUserCount());
        currentCounts.put("hrs", getHrUserCount());

        // 计算期初数量 = 当前数量 - 增长量
        Map<String, Long> baseCounts = new HashMap<>();
        baseCounts.put("jobSeekers", currentCounts.get("jobSeekers") - growth.get("jobSeekers"));
        baseCounts.put("companies", currentCounts.get("companies") - growth.get("companies"));
        baseCounts.put("hrs", currentCounts.get("hrs") - growth.get("hrs"));

        DecimalFormat df = new DecimalFormat("0.00%");
        Map<String, String> result = new HashMap<>();

        result.put("jobSeekers", calculatePercentage(growth.get("jobSeekers"), baseCounts.get("jobSeekers"), df));
        result.put("companies", calculatePercentage(growth.get("companies"), baseCounts.get("companies"), df));
        result.put("hrs", calculatePercentage(growth.get("hrs"), baseCounts.get("hrs"), df));

        return result;
    }

    private String calculatePercentage(long growth, long base, DecimalFormat df) {
        if (base == 0) {
            return growth == 0 ? "0.00%" : "N/A"; // 处理除零情况
        }
        double percentage = (double) growth / base;
        return df.format(percentage);
    }
}
